package cn.eastseven.demo.chat;

import org.springframework.stereotype.Component;

import cn.eastseven.demo.netty.common.Header;
import cn.eastseven.demo.netty.common.MessageType;
import cn.eastseven.demo.netty.common.NettyMessage;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelHandler.Sharable;

@Component
@Sharable
public class BusinessRespHandler extends ChannelHandlerAdapter {

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		NettyMessage req = (NettyMessage) msg;
		if(req.getHeader() != null && req.getHeader().getType() == MessageType.Business_REQ) {
			NettyMessage resp = new NettyMessage();
			Header header = new Header();
			header.setType(MessageType.Business_RESP);
			resp.setHeader(header);
			resp.setBody("HELLO WORLD");
			System.out.println("BusinessRespHandler.read: " + resp);
			ctx.writeAndFlush(resp);
		} else {
			ctx.writeAndFlush(msg);
		}
	}
}
